#!/usr/bin/python
import sys
sys.path.append("../classes")
from grouppulse import *

group_conf = {"fields": ["a","b","c"] , "splitter":"^^^" , "sumfields":{"Z1":"avg","Z2":"sum"} , "epocs":[3,10]}
compare_conf = {"minval" : 2 , "rates" : {"count":0.5 , "uage" : 0.7 , "uid": 0.7}}

GP = GroupPulse(group_conf)

#############################################################3333

lines = [{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.3" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"}]


print "GENERAL TEST"

for l in lines : GP.in_line(l)
GP.arithmetics()

print GP.Persist
sys.exit(0)

print "line assertion "   
assert(len(GP.Persist) == len(lines))
print "OK"

print "each line count == 1"
for k in GP.Persist : assert(GP.Persist[k]['count'] == 1)
print "OK"

print "in line #1 no Z1 assert persist line #1 has Z1 == 0.0"
assert(GP.Persist['aa^^^bb^^^cc']['Z1'] == 0.0)
print "OK"

print "###############################################################"

print "Multiplication TEST"

GP = GroupPulse(group_conf)
times = 10
for i in range(times):
	for l in lines : GP.in_line(l)
GP.arithmetics()

print "line assertion "
assert(len(GP.Persist) == len(lines))
print "OK"

print "each line count == %s" % (times)
for k in GP.Persist : assert(GP.Persist[k]['count'] == times)
print "OK"


print "line #0 Z2 = 0.3 assert %s * Z2 == %s" % (times , (times * GP.Persist['a^^^b^^^c']['Z2']))
assert(abs(3 - GP.Persist['a^^^b^^^c']['Z2']) < 0.01)
print "OK"

print "###############################################################"

print "Aggregation Test"

lines.append({"a":"a","b":"b","c":"c","d":"d","Z1":"15","Z2":"0.6" })

GP = GroupPulse(group_conf)
for l in lines : GP.in_line(l)
GP.arithmetics()

print "line assertion "
assert(len(GP.Persist) == len(lines) - 1)
print "OK"

print "assert line#0 Z1 is AVG"
assert(GP.Persist['a^^^b^^^c']['Z1'] == 10.0)
print "OK"

print GP.Persist['a^^^b^^^c']['Z2']

print "assert line#0 Z2 is SUM"
assert(abs(GP.Persist['a^^^b^^^c']['Z2'] - 0.9) < 0.01)
print "OK"

print "###############################################################"

print "mergeWith function Test"

lines = [{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.3" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"}]


gp1 = GroupPulse(group_conf)
for l in lines : gp1.in_line(l)
gp1.arithmetics()

gp2 = GroupPulse(group_conf)
gp1.mergeWith(gp2.Persist)

print "gp1 contains %s rows " % (len(lines))
assert(len( lines ) == len( gp1.Persist ))
print "OK"

print "gp2 contains 0 rows "
assert(0 == len( gp2.Persist ))
print "OK"

print "reverse : empty Master loads Slave..."
gp2.mergeWith(gp1.Persist)

print "gp2 contains %s rows " % (len(lines))
assert(len( lines ) == len( gp2.Persist ))
print "OK"

#############################################################3333

lines1 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.3" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"},
{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.1" },
{"a":"a","b":"b","c":"c","d":"d","Z1":"3","Z2":"0.2" },
{"a":"a","b":"b","c":"c","d":"d","Z1":"2","Z2":"0.3" },]

lines2 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"15","Z2":"0.6" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"}]

print "GENERAL TEST"

g1 = GroupPulse(group_conf)
g2 = GroupPulse(group_conf)

for l in lines1 : g1.in_line(l)
g1.arithmetics()
for l in lines2 : g2.in_line(l)
g2.arithmetics()


print "asserting aggregation on g1: assert in k a^^^b^^^c z2 = 0.9"
assert(abs(g1.Persist['a^^^b^^^c']['Z2'] - 0.9) < 0.01)
print "OK"

print "asserting aggregation on g1: assert in k a^^^b^^^c z1 = 15 / 4"
assert(abs(g1.Persist['a^^^b^^^c']['Z1'] - (15.0/4.0)) < 0.01)
print "OK"



